package com.os.opencv.java.chapter4;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

public class Submat {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat src1 = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/view.jpg");

        /*HighGui.imshow("图像1", src1);
        HighGui.waitKey(0);*/

        //创建子矩阵并设置为蓝色
        Mat sub1 = src1.submat(380, 600, 350, 610);
        Scalar blue = new Scalar(255, 0, 0);
        sub1.setTo(blue);

        //在屏幕上显示设置子矩阵后的图像1
        /*HighGui.imshow("设置子矩阵后的图像1", src1);
        HighGui.waitKey(0);*/

        //读取图像2并在屏幕上显示
        Mat src2 = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/view.jpg");
        HighGui.imshow("图像2", src2);
        HighGui.waitKey(0);

        //设置子矩阵
        Rect roi = new Rect(150, 600, 400, 600);
        Mat sub2 = src2.submat(roi);

        //将子矩阵中RGB颜色值改为原来的一半
        for(int i=0; i<sub2.rows(); i++){
            for(int j=0; j<sub2.cols(); j++){
                byte[] data = new byte[3];
                sub2.get(i, j, data);
                for(int n=0; n<3; n++){
                    data[n] = (byte) (data[n]/2);
                }
                sub2.put(i, j, data);
            }
        }

        //在屏幕上显示子矩阵
        HighGui.imshow("子矩阵2", sub2);
        HighGui.waitKey(0);

        //在屏幕上显示设置子矩阵后的图像2
        HighGui.imshow("设置子矩阵后的图像2", src2);
        HighGui.waitKey(0);

        //读取图像3
        Mat src3 = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/view.jpg");
        //创建子矩阵并设置为蓝色
        Mat sub3 = src3.submat(new Range(400, 600), new Range(450, 800));
        sub3.setTo(blue);

        //查看子矩阵信息并在屏幕上显示设置子矩阵后的图像3
        HighGui.imshow("设置子矩阵后的图像3", src3);
        HighGui.waitKey(0);

        //查看三个子矩阵信息
        System.out.println(sub1);
        System.out.println(sub2);
        System.out.println(sub3);

        System.exit(0);
    }
}
